//
//  MyControllerd.m
//  Gift
//
//  Created by dllo on 15/9/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyControllerd.h"
#import "BaseHeader.h"
#import "LDAboutController.h"
#import "LDheartController.h"
#import "LDMyView.h"

@interface MyControllerd ()

@property (nonatomic, retain)NSString *str;
@property (nonatomic, retain)UISwitch *swit;
//通过颜色来分辨夜间模式使用的View
@property (nonatomic, retain)UIButton *heartButton;
@property (nonatomic, retain)LDMyView *ldMyView;

@end

@implementation MyControllerd

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.str release];
    [self.swit release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.ldMyView = [[LDMyView alloc]init];
    [self.view addSubview:self.ldMyView];
    
    [self createheartButton];
    [self createrView];
    self.title = @"我的";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
       
        [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];
         self.view.backgroundColor = [UIColor colorWithRed:0.572 green:0.218 blue:1.000 alpha:1.000];

    }
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    }

//收藏页按钮
- (void)createheartButton{
    
    self.heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.heartButton.frame = CGRectMake(SCREEN_WIDTH - 100, SCREEN_HEIGHT / 2 +10, 30, 30);
    [self.heartButton setImage:[UIImage imageNamed:@"xin-3"] forState:UIControlStateNormal];
    [self.heartButton addTarget:self action:@selector(handleButton) forControlEvents:UIControlEventTouchUpInside];
//    self.heartButton.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.heartButton];
}
//收藏页  点击方法;
- (void)handleButton{
    LDheartController *heartView = [[LDheartController alloc]init];
    [self.navigationController pushViewController:heartView animated:YES];
    [heartView release];
}


//缓存点击方式
- (void)handleButton:(UIButton *)selected{
   
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
    
    self.str = [NSString stringWithFormat:@"缓存大小为%.2fMB", [self folderSizeAtPath:cachePath]];
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"清除缓存"
    message:self.str preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
    
    [alert addAction:defaultAction];
    UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        //获取完整路径
        NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
        path = [path stringByAppendingPathComponent:@"Caches"];
        NSFileManager *manager = [NSFileManager defaultManager];
        if ([manager fileExistsAtPath:path]) {
            NSArray *fileNameArray = [manager subpathsAtPath:path];
            for (NSString *fileName in fileNameArray) {
                //拼接绝对路径
                NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
                //通过文件管理者删除文件
                [manager removeItemAtPath:absolutePath error:nil];
                
            }
        }
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];

}

- (long long)fileSizeAtPath:(NSString *)path{
    //创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}
#pragma mark -  计算一个文件夹大小
- (float)folderSizeAtPath:(NSString *)path{
    //创建文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumerator = [[manager subpathsAtPath:path] objectEnumerator];
    
    //每次遍历得到的文件名
    NSString *fileName = [NSString string];
    
    //文件夹大小
    float folderSize = 0;
    while ((fileName = [enumerator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return  folderSize / (1024.0 * 1024.0);
}




//注册消息中心
- (void)handleSwit:(UISwitch *)swit{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    
    if ([passWord isEqualToString:@"yes"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"lidi" object:@"no"];
        //轻量级存储改变为NO
        [user setObject:@"no" forKey:@"night"];
        [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
        self.view.backgroundColor = [UIColor whiteColor];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"lidi" object:@"yes"];
        //轻量级存储改为YES
        [user setObject:@"yes" forKey:@"night"];
        [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];
        self.view.backgroundColor = [UIColor colorWithRed:0.572 green:0.218 blue:1.000 alpha:1.000];
       
    }

}



- (void)createrView{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    [button setTitle:@"缓存" forState:UIControlStateNormal];
    button.frame = CGRectMake(SCREEN_WIDTH - 200, SCREEN_HEIGHT / 2 + 168, 100, 30);

    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    //夜间模式
    self.swit = [[UISwitch alloc]init];
    self.swit.frame = CGRectMake(SCREEN_WIDTH - 100, SCREEN_HEIGHT / 2 + 48, 30, 30);

    [self.swit addTarget:self action:@selector(handleSwit:) forControlEvents:UIControlEventValueChanged];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"night"] isEqualToString:@"yes"]) {
        [self.swit setOn:1];
        self.view.backgroundColor = [UIColor blackColor];
    }
    [self.view addSubview:self.swit];
      //分享
    
    UIButton *buttonB = [UIButton buttonWithType:UIButtonTypeCustom];
//    [buttonB setTitle:@"分享" forState:UIControlStateNormal];
    buttonB.frame = CGRectMake(SCREEN_WIDTH - 200, SCREEN_HEIGHT / 2 + 88, 100, 30);
    [buttonB setTintColor:[UIColor blackColor]];
    [buttonB addTarget:self action:@selector(handleButtonBlue) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: buttonB];
    //关于我们
        UIButton *buttonR = [UIButton buttonWithType:UIButtonTypeCustom];
//    [buttonR setTitle:@"关于我们" forState:UIControlStateNormal];
    buttonR.frame = CGRectMake(SCREEN_WIDTH - 200, SCREEN_HEIGHT / 2 + 128, 100, 30);

    [buttonR addTarget:self action:@selector(handleButtonBlueRed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonR];
    
}

//分享点击
- (void)handleButtonBlue{
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:@"你要分享的文字" shareImage:[UIImage imageNamed:@"icon.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToWechatSession,UMShareToQQ,nil]delegate:self];
    
}

//关于我们
- (void)handleButtonBlueRed{
    LDAboutController *abuot = [[LDAboutController alloc]init];
    [self.navigationController pushViewController:abuot  animated:YES];
    [abuot release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
